﻿using BLL;
using MODEL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Loginter
{
    public partial class AddTruckList : System.Web.UI.Page
    {
        //调用BLL层查询车辆信息
        SelectTruckBLL truck = new SelectTruckBLL();
        //调用BLL层的车队有关内容
        TruckTeamBLL truckteam = new TruckTeamBLL();
        //调用BLL层的车辆信息
        TruckBLL truckbll = new TruckBLL();

        /// <summary>
        /// 页面运行时加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindtruckList();
                BindcheliangList();
            }
        }
        /// <summary>
        /// 绑定所属车队名称
        /// </summary>
        private void BindtruckList()
        {
            //datatable类型的车队信息
            DataTable table = truckteam.GetTruckList();
            //车队名称的绑定的位置
            drop_TruckName.DataSource = table;
            drop_TruckName.DataTextField = "TeamName";
            drop_TruckName.DataValueField = "TeamID";
            //绑定
            drop_TruckName.DataBind();
            drop_TruckName.Items.Insert(0, new ListItem("不限", "0"));
        }
        /// <summary>
        /// 绑定车辆类型
        /// </summary>
        private void BindcheliangList()
        {
            DataTable table = truck.GetTruckList();
            drop_TruckType.DataSource = table;
            drop_TruckType.DataTextField = "Type";
            drop_TruckType.DataValueField = "Type";
            drop_TruckType.DataBind();
            drop_TruckType.Items.Insert(0, new ListItem("--全部--", "0"));
        }
        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_TruckAdd_Click(object sender, EventArgs e)
        {
            BindTruckList();
        }
        /// <summary>
        /// 绑定车辆的信息
        /// </summary>
        private void BindTruckList()
        {
            //车牌号码
            string num = txt_TruckNumber.Text.Trim();
            //车辆类型
            string tepe = Convert.ToString(drop_TruckType.SelectedValue);
            //车辆长度
            string leng = txt_Length.Text.Trim();
            //车辆吨位
            int age = Convert.ToInt32(txt_Tonngae.Text.Trim());
            //车队名称
            int teamid = Convert.ToInt32(drop_TruckName.SelectedValue);
            //购买时间
            DateTime data = Convert.ToDateTime(txt_BuyTim.Value.Trim());
            //备注
            string mark = txt_Remark.Text.Trim();
            //类型
            int sta = 1;
            //定义时间
            string time = DateTime.Now.ToShortDateString();
            TruckTeamQueryModel tt = new TruckTeamQueryModel()
            {
                FK_TeamID = teamid,
                Number = num,
                BuyDate = data,
                Type = tepe,
                Length = leng,
                Tonngae = age,
                State = sta,
                Remark = mark,

                CheckInTime = Convert.ToDateTime(time),
                AlterTime = Convert.ToDateTime(time)
            };
            int result = truckbll.UInsertTruck(tt);
            if (result > 0)
            {
                //跳转页面到用户信息
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加成功！');location.href='TruckInfoList.aspx';</script>");
            }
            else
            {
                lab_Add.Text = "添加失败！";
            }
        }
        /// <summary>
        /// 重置按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_TruckFales_Click(object sender, EventArgs e)
        {
            txt_TruckNumber.Text = null;
            drop_TruckName.SelectedValue = null;
            drop_TruckType.SelectedValue = null;
            txt_BuyTim.Value = "";
            txt_Length.Text = "";
            txt_Tonngae.Text = "";
            txt_Remark.Text = "";
        }
    }
}